import { createPinia, defineStore } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'

const store = createPinia()
store.use(piniaPluginPersistedstate)

export const useRootStore = defineStore('root', {
    state: () => ({
        count: 0
    })
})

// 修复用户Store：添加userInfo和isLogin状态，完善actions
export const useUserStore = defineStore('user', {
    state: () => ({
        isLogin: false, // 登录状态
        userInfo: {     // 用户信息对象
            avatar: '',
            nickname: '',
            username: ''
        }
    }),
    actions: {
        // 登录动作：保存用户信息
        login(userInfo) {
            this.isLogin = true;
            this.userInfo = { ...userInfo };
        },
        // 登出动作：清空用户信息
        logout() {
            this.isLogin = false;
            this.userInfo = { avatar: '', nickname: '', username: '' };
        }
    },
    persist: {
        storage: localStorage,
        paths: ['isLogin', 'userInfo'] // 持久化关键字段
    }
})

export default store